//收藏问题
function myCollect(number) {

    if($("#uid").value == undefined){
        loginPop();
    }else{
        var questionUpdateVO={number:number}

        $.ajax({
            url:"collectIssue",
            type:"POST",
            contentType:"application/json",
            data:JSON.stringify(questionUpdateVO),
            success:function (data) {
                if(data == 1){
                    layer.msg("收藏成功");
                    $("#tupian"+number).attr("src","/static/image/shoucang.png");
                    var str=$("#collectType"+number).text().split("(")[1];
                    var str1=str.split(")")[0];
                    var count= Number(str1)+1;
                    $("#collectType"+number).text("收藏问题("+count+")");
                }else if(data == 2){
                    layer.msg("系统错误");
                }else if(data == 3){
                    layer.msg("您已收藏此问题，不可重复收藏");
                    $("#tupian"+number).attr("src","/static/image/shoucang.png");
                }else if(data == 4){
                    loginPop();
                }
            }
        })
    }
}

//显示评论
function commentInit(nid) {

    console.log(nid);

    if($(".pinglun"+nid).attr("style") == 'display: none'){
        $(".pinglun"+nid).show();
    }
    if($(".pinglun").attr("style")!='display: none'){
        var discussPO = {nid:nid,typees:1}
        $.ajax({
            url:"nidQuery",
            type:"POST",
            contentType:"application/json",
            data:JSON.stringify(discussPO),
            success:function (data) {
                $("#commentLists"+nid).html(data);
            }
        })
    }
}

//回答的点赞
function addOption(id,gid,iid) {//gid用来改变样式为高亮,iid是问题id
    if($("#uid").value == undefined){
        loginPop();
    }else{
        var likes={newsid:iid,did:id,sort:1,type:1}

        $.ajax({
            url:"addTags",
            type:"POST",
            contentType:"application/json",
            data:JSON.stringify(likes),
            success:function (data) {
                if(data == 1){
                    layer.msg("点赞成功");
                    var str=$("#qaReplyLikeCount"+id).text();
                    str=str.substring( 0 ,str.length - 2 );
                    $("#qaReplyLikeCount"+id).text(Number(str)+1+"点赞");
                }else if(data == 0){
                    layer.msg("您已点赞，不可重复点赞");
                }else if(data == 2){
                    loginPop();
                }
            }
        })
    }
}

//回答的点踩
function addTread(id,cid) {
    if($("#"+cid).attr("src") == '/static/image/icon_Like-active.png'){
        layer.msg("您已点踩，不可重复点踩");
        return;
    }

    $.ajax({
        url:"tread/"+id,
        type:"GET",
        contentType:"application/json",
        success:function (data) {
            if(data == 1){
                layer.msg("点踩成功");
                var str= $("#qaReplyStepCount"+id).text();
                str=str.substring( 0 ,str.length - 1 );
                $("#qaReplyStepCount"+id).text(Number(str)+1+"踩");

                $("#"+cid).attr("src","/static/image/icon_Like-active.png");

            }
        }
    })
}

//发表回答
function publish(obj) {//id是问题id，没用到，可以不用

    var qid=obj.getAttribute("qid");

    editor.sync();

    if($("#uid").value == undefined){
        loginPop();
    }else{
        if($("#editor_id").val().length==0||$("#editor_id").val() == ''){
            layer.msg("请输入内容");
            return;
        }

        if($("#editor_id").val().length > 200){
            layer.msg("图片超出限制");
            return;
        }

        var answerAudit={

            content:$("#editor_id").val(),//内容

            issue:$(".qa-title").text(),//问题

        }

        $.ajax({
            url:"addAnswerAudit",
            type:"POST",
            contentType:"application/json",
            data:JSON.stringify(answerAudit),
            success:function (data) {
                if(data == 1){
                    layer.msg("恭喜您回答成功，审核通过即可查看");
                    editor.html("");
                }else{
                    layer.msg("回答失败，请稍后重试");
                }
            }
        })
    }


}

//点击查看评论的回复
function discussClick(id,nid,num,reply) {//num是标记用来加载页面时用到,reply是当再点击收起回复的时候获取的回复条数

    var discussPo={
        fid:id,
        nid:nid,
        typees:1
    }

    if($("#code"+id+" span").text() == "收起回复"){
        $("#code"+id+" span").text(reply+"条回复");
        $("#"+num).hide();
        return;
    }

    $("#"+num).show();

    $.ajax({
        url:"fidQueryDiscuss",
        type:"POST",
        contentType:"application/json",
        data:JSON.stringify(discussPo),
            success:function (data) {
                $("#code"+id+" span").text("收起回复");
                $("#"+num).html(data);
            }
    })

}


//发表的评论
function btdAddComment(reply,nid) {
    if($("#uid").value == undefined){
        loginPop();
    }else{
        if($("#"+reply).val()=='' ||$("#"+reply).val().length ==0){
            layer.msg("内容不能为空");
            return;
        }

        var discuss={
            /* fid:$(".leave-message").attr("fid"),*/
            nid:nid,
            /* uid:13,*/
            content:$("#"+reply).val(),
            typees:1
        }

        $.ajax({
            url:"addDisscuss",
            type:"POST",
            contentType:"application/json",
            data:JSON.stringify(discuss),
            success:function (data) {
                if(data.success == true){
                    layer.msg(data.msg)
                    window.location.reload();
                }else{
                    layer.msg(data.msg);
                }
            }
        })
    }


}

//对评论点击回复展示输入框
function replys(id) {

    var list = document.getElementsByClassName("posi-input");
    for (var i = 0; i < list.length; i++) {
        list[i].setAttribute("style","display: none;");
    }

    document.getElementById(id).setAttribute("style","");

/*    if($("#"+id).attr("style") == ''){
        $("#"+id).hide();
        return;
    }
    $("#"+id).show();*/

}

//回复评论
function revert(id,nid) {
    if($("#uid").value == undefined){
        loginPop();
    }else{
        var disscuss= {
            fid:id,
            nid:nid,
            content:$("#reply"+id).val(),
        }

        $.ajax({
            url:"addReplyDisscuss",
            type:"POST",
            contentType:"application/json",
            data:JSON.stringify(disscuss),
            success:function (data) {
                if(data.success == true){
                    alert(data.msg)
                    window.location.reload();
                }else{
                    alert(data.msg);
                }
            }
        })
    }

}


//评论的点赞
function addlike(id,nid) {

    var addlikes={
        did:id,
        newsid:nid,
        sort:1,
        type:1
    }

    $.post("/question/addDiessTags",addlikes,function (data,e) {
        if(data == 1){
            layer.msg("点赞成功");
            $("#dianzan"+id).text(Number($("#dianzan"+id).text())+1);
           /* if($(e.target).attr("id")== $("#dianzan"+id)){
            }else if($(e.target).attr("id")== $("#huifupinglun"+id)){
                $("#huifupinglun"+id).text(Number($("#huifupinglun"+id).text())+1);
            }*/
        }else if(data == 0){
            layer.msg("您已点赞，不可重复点赞");
        }else if(data == 2){
            loginPop();
        }
    })

}


//评论的评论的点赞
function addPinglunLikes(id,nid) {
    var addlikes={
        did:id,
        newsid:nid,
        sort:1,
        type:1
    }

    $.post("/question/addDiessTags",addlikes,function (data,e) {
        if(data == 1){
            layer.msg("点赞成功");
            $("#huifupinglun"+id).text(Number($("#huifupinglun"+id).text())+1);
        }else if(data == 0){
            layer.msg("您已点赞，不可重复点赞");
        }else if(data == 2){
            loginPop();
        }
    })

}


//弹窗登录
function loginPop(){
    var w="400px",h="570px";
    if(window.screen.width < 480){//移动端设置宽高
        w='95%';
        h='70%';
    }
    layer.open({
        type: 2,
        title: false,
        shadeClose: true,       //开启遮罩关闭
        area: [w, h],
        fixed: true,            //不固定
        maxmin: false,           //缩放
        content: basePath+'login?isd=1',//弹窗登录必填isd=1
    });
}

